/*
 * Copyright 2007 The semap project development team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Created by myn at 06.08.2007 12:40:20.
 *
 * CommandProcessor class.
 *
 * $Id: CommandProcessor.java 420 2007-08-22 11:36:45Z Mike.Navern $
 */

package com.google.code.contactserv;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import com.google.code.contactserv.model.PropertyHolder;
import com.google.code.contactserv.service.ContactService;

public abstract class CommandProcessor {
    protected ContactService        srv  = null;
    protected List<PropertyHolder>  lst  = null;
    protected String                lang = null; 

    // constants
    public static final int BUFSIZE = 512;

    /**
     * @param srv
     * @param line
     */
    public CommandProcessor(ContactService srv, List<PropertyHolder> lst) {
        super();
        this.srv = srv;
        this.lst = lst;
    }

    abstract public void doWork(String uri);
    
    public void printIntroduction() {
    }

    public void printPrompt() {
        System.out.print("uri>");
    }

    public void printUriError(String str) {
        System.out.print("Wrong URI provided [" + str + "]");
    }

    public void run() {
        String str  = "";
        lang = setupLanguage();

        try {
            printPrompt();
            while (!(str = readNext()).isEmpty()) {
                if (!isURI(str)) {
                    printUriError(str);
                } else {
                    doWork(str);
                }
                printPrompt();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public boolean isURI(String uri) {
        boolean rc = true;
        try {
            new URI(uri);
        } catch (URISyntaxException e) {
            rc = false;
        }
        return rc;
    }

    protected String readNext() throws IOException {
        byte[] buf = new byte[BUFSIZE];
        int sz = 0;
        String str = "";

        sz = System.in.read(buf, 0, BUFSIZE);
        if (sz != -1) {
            str = new String(buf, 0, sz);
            str = str.trim();
        }
        return str;
    }
    
    /**
     * @return Language identifier 
     */
    protected String setupLanguage() {
        PropertyHolder etalon = new PropertyHolder("language", "");
        String lang = "";

        if (lst.contains(etalon)) {
            lang = lst.get(lst.indexOf(etalon)).getValue();
        }
        return lang;
    }
}
